package com.pyg.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pyg.manager.service.SpecificationService;
import com.pyg.pojo.TbSpecification;
import com.pyg.utils.PageResult;
import com.pyg.utils.PygResult;
import com.pyg.vo.Specification;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/specification")
public class SpecificationController {

    @Reference(timeout = 1000000)
    private SpecificationService specificationService;


    @RequestMapping("/findByPage/{page}/{rows}")
    public PageResult findByPage(@PathVariable Integer page,
                                 @PathVariable Integer rows) {
        PageResult result = specificationService.findByPage(page, rows);
        return result;
    }

    @RequestMapping("/add")
    public PygResult add(@RequestBody Specification specification) {
        try {
            specificationService.insert(specification);
            return new PygResult(true, "保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false, "保存失败!");
        }
    }

    @RequestMapping("/findById")
    public Specification findById(Long id) {
        return specificationService.selectByPrimaryKey(id);
    }

    @RequestMapping("/update")
    public PygResult update(@RequestBody Specification specification) {
        try {
            specificationService.update(specification);
            return new PygResult(true, "保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false, "保存失败!");
        }
    }

    @RequestMapping("/delete/{ids}")
    public PygResult delete(@PathVariable Long[] ids) {
        try {
            specificationService.delete(ids);
            return new PygResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false, "删除失败");
        }
    }
}
